# Maintainer: Junjie Mao <eternal.n08@gmail.com>


_realname=flexdll
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.34
pkgrel=2
pkgdesc="An implementation of a dlopen-like API for Windows (mingw-w64)"
arch=('any')
url='http://alain.frisch.fr/flexdll.html'
license=('zlib/libpng License')
makedepends=("${MINGW_PACKAGE_PREFIX}-ocaml")
source=("http://alain.frisch.fr/flexdll/${_realname}-${pkgver}.tar.gz"
        "0001-Fix-the-List.hd-exception.patch"
        "0002-Add-path-to-libcrt2.o-into-the-default-library-searc.patch"
        "0003-Remove-the-usages-of-stdout-to-workaround-the-missin.patch"
        "0004-use-lib-flexdll-as-the-default-library-path.patch")
sha256sums=('db5e2e97c17ddc9dbb12835b9265e59eeab892fd62d15dcc13f176730b875117'
            '58df96e5459605342fc6e60a442e23de3841a50743cb2e5a36d3be78d7015fe1'
            '74aaa64d7c7309393092f64fbd7daf6d6f07f300c98ad1194a028d8d89ae96f7'
            '3d9470049d1e8070861cf344b6e31caca89c98ab9801483b48d69e81a45c1d1e'
            'c5e6d9891557866d1dfaed3a693dfaa294babfbecec1970ac2f51ae9eabc10d8')

if [ "${CARCH}" == "i686" ]; then
  chain="mingw"
else
  chain="mingw64"
fi

prepare() {
  cd ${srcdir}/${_realname}

  patch -p1 -i ${srcdir}/0001-Fix-the-List.hd-exception.patch
  patch -p1 -i ${srcdir}/0002-Add-path-to-libcrt2.o-into-the-default-library-searc.patch
  patch -p1 -i ${srcdir}/0003-Remove-the-usages-of-stdout-to-workaround-the-missin.patch
  patch -p1 -i ${srcdir}/0004-use-lib-flexdll-as-the-default-library-path.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf ${_realname} build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  unset FLEXDIR
  unset OCAMLLIB
  make flexlink.exe -j1
  make build_${chain} -j1
}

check() {
  # The test demo works on x86_64 but fails on i686. Root causes of the failure
  # is still unknown, but the tool is good enough to be used in other
  # applications such as ocaml and coq.
  plain "skip"
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}

  install -Dm755 flexlink.exe ${pkgdir}${MINGW_PREFIX}/bin/flexlink.exe
  for f in flexdll_${chain}.o flexdll_initer_${chain}.o flexdll.h flexdll.c flexdll_initer.c default.manifest default_amd64.manifest; do
    install -D ${f} ${pkgdir}${MINGW_PREFIX}/lib/flexdll/${f}
  done
  [ -f LICENSE ] && install -Dm644 LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  [ -f CHANGES ] && install -Dm644 CHANGES ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/CHANGES
}
